var lastTabId = 0;

chrome.tabs.onSelectionChanged.addListener(function(tabId) {
	lastTabId = tabId;
	var extensionStatus = localStorage["extensionStatus"];
	var icon = 'fr.ico';
	if (extensionStatus == 'off') {
		icon = 'fr_off.ico';
	}
	chrome.pageAction.setIcon({
		path : icon,
		tabId : tabId
	});
});

chrome.tabs.getSelected(null, function(tab) {
	lastTabId = tab.id;
	chrome.pageAction.show(lastTabId);
});

// Called when the url of a tab changes.
function checkForValidUrl(tabId, changeInfo, tab) {
	// chrome.pageAction.show(tabId); // dev
	if (tab.url.indexOf('http://www.ryanair.com') == 0
			|| tab.url.indexOf('https://www.bookryanair.com') == 0) {
		var extensionStatus = localStorage["extensionStatus"];
		var icon = 'fr.ico';
		if (extensionStatus == 'off') {
			icon = 'fr_off.ico';
		}
		chrome.pageAction.setIcon({
			path : icon,
			tabId : tabId
		});
		chrome.pageAction.show(tabId);
	}
};

// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);

function onRequest(request, sender, sendResponse) {

	if (location.hostname == sender.id && request.fieldId != undefined) {
		sendResponse({
			"fieldValue" : localStorage[request.fieldId] !=undefined ? localStorage[request.fieldId] : "",
			"versionId" : chrome.i18n.getMessage("@@extension_id")
		});
	}
};
// Return nothing to let the connection be cleaned up.

// Listen for the content script to send a message to the background page.
chrome.extension.onRequest.addListener(onRequest);

function onInstall() {
	console.log("Extension Installed");
	localStorage['skipServicesChange'] = 'off';
	localStorage['skipHotels'] = 'off';
}

function onUpdate(prevVersion) {
	if(localStorage['skipServicesChange']==undefined){
		localStorage['skipServicesChange']='off';
	}
	
	if(localStorage['skipHotels'] == undefined){
		localStorage['skipHotels'] = 'off';
	}

	if(prevVersion.indexOf("1.") ==0 || prevVersion.indexOf("2.0.0") ==0){
		dataMigrationFrom1to2();
	}

	if(prevVersion.indexOf("2.0.") ==0){
		clearingUndefinedValues_Bugfix();
	}
	
	console.log("Ryanair Better Booking extension updated");
}

function getVersion() {
	var details = chrome.app.getDetails();
	return details.version;
}

// Check if the version has changed.
var currVersion = getVersion();
var prevVersion = localStorage['version']
if (currVersion != prevVersion) {
	// Check if we just installed this extension.
	if (typeof prevVersion == 'undefined') {
		onInstall();
	} else {
		onUpdate(prevVersion);
	}
	localStorage['version'] = currVersion;
}
function clearingUndefinedValues_Bugfix(){

	clearIfUndefined("firstname");
	clearIfUndefined("lastname");
	clearIfUndefined("title");
	clearIfUndefined("bags_0_0");
	clearIfUndefined("PaymentInputPaymentView_Email,#EmailConfirm");
	clearIfUndefined("PhoneCountry");
	clearIfUndefined("AreaCode");
	clearIfUndefined("PhoneNumber");
	clearIfUndefined("PaymentInputPaymentView_Country");
	clearIfUndefined("PaymentInputPaymentView_FirstName");
	clearIfUndefined("PaymentInputPaymentView_LastName");
	clearIfUndefined("PaymentInputPaymentView_Title");
	clearIfUndefined("PaymentInputPaymentView_ACCTNO");
	clearIfUndefined("PaymentInputPaymentView_Addr1");
	clearIfUndefined("PaymentInputPaymentView_Addr2");
	clearIfUndefined("PaymentInputPaymentView_Addr3");
	clearIfUndefined("PaymentInputPaymentView_City");
	clearIfUndefined("PaymentInputPaymentView_PostCode");
}

function clearIfUndefined(name){
	if(localStorage[name]=="undefined"){
		localStorage[name]="";
	}
}

function dataMigrationFrom1to2(){
	localStorage["firstname"] = localStorage["GroupServices_PassengerInputServices_TextBoxFirstName_1"];
	localStorage["lastname"] = localStorage["GroupServices_PassengerInputServices_TextBoxLastName_1"];
	localStorage["title"] = localStorage["GroupServices_PassengerInputServices_DropDownListTitle_1"];
	localStorage["bags_0_0"] = localStorage["SSRCode_PASSENGERCOUNT_0_SSRCOUNT_0"];
	localStorage["PaymentInputPaymentView_Email,#EmailConfirm"] = localStorage["PaymentLayoutView_PaymentInputCCFRPaymentView_ContactInputViewPaymentView_TextBoxEmailAddress, #email_conf, #BookingRetrieveInputFRSecurityView_CONTACTEMAIL1"];
	localStorage["PhoneCountry"] = localStorage["alt_phone_a"];
	localStorage["AreaCode"] = localStorage["alt_phone_b"];
	localStorage["PhoneNumber"] = localStorage["alt_phone_c"];
	localStorage["PaymentInputPaymentView_Country"] = localStorage["PaymentLayoutView_PaymentInputCCFRPaymentView_TextBoxCOUNTRY"];
	localStorage["PaymentInputPaymentView_FirstName"] = localStorage["PaymentLayoutView_PaymentInputCCFRPaymentView_ContactInputViewPaymentView_TextBoxFirstName"];
	localStorage["PaymentInputPaymentView_LastName"] = localStorage["PaymentLayoutView_PaymentInputCCFRPaymentView_ContactInputViewPaymentView_TextBoxLastName"];
	localStorage["PaymentInputPaymentView_Title"] = localStorage["PaymentLayoutView_PaymentInputCCFRPaymentView_ContactInputViewPaymentView_DropDownListTitle"];
	localStorage["PaymentInputPaymentView_ACCTNO"] = localStorage["PaymentLayoutView_PaymentInputCCFRPaymentView_TextBoxACCTNO"];
	localStorage["PaymentInputPaymentView_Addr1"] = localStorage["PaymentLayoutView_PaymentInputCCFRPaymentView_TextBoxADDR1"];
	localStorage["PaymentInputPaymentView_Addr2"] = localStorage["PaymentLayoutView_PaymentInputCCFRPaymentView_TextBoxADDR2"];
	localStorage["PaymentInputPaymentView_Addr3"] = localStorage["PaymentLayoutView_PaymentInputCCFRPaymentView_TextBoxADDR3"];
	localStorage["PaymentInputPaymentView_City"] = localStorage["PaymentLayoutView_PaymentInputCCFRPaymentView_TextBoxCITY"];
	localStorage["PaymentInputPaymentView_PostCode"] = localStorage["PaymentLayoutView_PaymentInputCCFRPaymentView_TextBoxPOSTAL"];
	

/*
	localStorage["undefined"] = localStorage["ControlGroupFRSegmentsView_SubControlGroupFRSegmentsView_PassengerInputViewFRSegmentsView_DropDownListBirthDateMonth_1"];
	localStorage["undefined"] = localStorage["ControlGroupFRSegmentsView_SubControlGroupFRSegmentsView_PassengerInputViewFRSegmentsView_DropDownListBirthDateYear_1"];
	localStorage["undefined"] = localStorage["ControlGroupFRSegmentsView_SubControlGroupFRSegmentsView_PassengerInputViewFRSegmentsView_DropDownListBirthDateDay_1"];
	localStorage["undefined"] = localStorage["ControlGroupFRSegmentsView_SubControlGroupFRSegmentsView_PassengerInputViewFRSegmentsView_DropDownListNationality_1"];
	localStorage["undefined"] = localStorage["ControlGroupFRSegmentsView_SubControlGroupFRSegmentsView_PassengerInputViewFRSegmentsView_DropDownListDocumentType1_1"];
	localStorage["undefined"] = localStorage["ControlGroupFRSegmentsView_SubControlGroupFRSegmentsView_PassengerInputViewFRSegmentsView_TextBoxDocumentNumber1_1"];
	localStorage["undefined"] = localStorage["ControlGroupFRSegmentsView_SubControlGroupFRSegmentsView_PassengerInputViewFRSegmentsView_DropDownListDocumentCountry1_1"];
	localStorage["undefined"] = localStorage["ControlGroupFRSegmentsView_SubControlGroupFRSegmentsView_PassengerInputViewFRSegmentsView_DropDownListDocumentDateDay1_1"];
	localStorage["undefined"] = localStorage["ControlGroupFRSegmentsView_SubControlGroupFRSegmentsView_PassengerInputViewFRSegmentsView_DropDownListDocumentDateMonth1_1"];
	localStorage["undefined"] = localStorage["ControlGroupFRSegmentsView_SubControlGroupFRSegmentsView_PassengerInputViewFRSegmentsView_DropDownListDocumentDateYear1_1"];
*/
}
